package com.HRs.utils;
/*
    JDBC 工具类
 */

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private JDBCUtils(){}

    private static String driverClass;
    private static String url;
    private static String username;
    private static String password;
    private static Connection con;

    static {
        try{
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("config.properties");
            Properties prop = new Properties();
            prop.load(is);

            driverClass = prop.getProperty("driverClass");
            url = prop.getProperty("url");
            username = prop.getProperty("username");
            password = prop.getProperty("password");

            Class.forName(driverClass);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        try{
            con = DriverManager.getConnection(url, username, password);

        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return con;
    }

    public static void close(Connection con, Statement st, ResultSet rs){
        if(con != null){
            try{
                con.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(st != null){
            try{
                st.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(rs != null){
            try{
                rs.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection con, Statement st){
        if(con != null){
            try{
                con.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(st != null){
            try{
                st.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}
